// 判断安卓、ios
function getDevice() {
  var u = navigator.userAgent
  var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1 // android终端
  var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/) // ios终端
  var result
  if (isAndroid) {
    result = 'android'
  }
  if (isiOS) {
    result = 'ios'
  }
  return result
}

// web端：根据不同系统执行关闭方法
function NextStep() {
  var env = getDevice()
  if (env == 'android') {
    window.APPCALL.__BSNLAPPLinkBridge__ = function(jsonString) { console.log(jsonString) }
    window.APPCALL.__BSNLAPPLinkBridge__(
      JSON.stringify({ type: 'bihuasuan' })
    )
    window.closeWebView()
  } else if (env == 'ios') {
    __BSNLAPPLinkBridge__()
  }
}
function getParam(paramName) {
  (paramValue = ''), (isFound = !1)
  if (
    window.location.search.indexOf('?') == 0 &&
        window.location.search.indexOf('=') > 1
  ) {
    (arrSource = unescape(window.location.search)
      .substring(1, window.location.search.length)
      .split('&')),
    (i = 0)
    while (i < arrSource.length && !isFound) {
      arrSource[i].indexOf('=') > 0 &&
                arrSource[i].split('=')[0].toLowerCase() ==
                paramName.toLowerCase() &&
                ((paramValue = arrSource[i].split('=')[1]), (isFound = !0)),
      i++
    }
  }
  return paramValue == '' && (paramValue = null), paramValue
}

// alert("初始化授权页面");
window.onload = function() {
  var $error = document.getElementById('error')
  var $p1 = document.getElementById('p1')
  var $p2 = document.getElementById('p2')
  var $maskWrapper = document.getElementsByClassName('mask-wrapper')[0]
  var $authorizationWrapper = document.getElementsByClassName('authorization-wrapper')[0]
  var $success = document.getElementById('success')
  var $btn = this.document.getElementById('btn')

  // 根据环境不同获取api地址
  function getApiUrl() {
    // alert("开始获取平台参数");
    var platform = getParam('state')
    var code = getParam('code')
    // alert(`平台参数获取成功${platform}`);
    if (!platform || !code) {
      return
    }
    if (platform.match('dev')) {
      // alert("当前环境dev");
      return 'http://172.16.6.126:8423/auth/taobao/create?code=' + code + '&state=' + platform
    } else if (platform.match('uat')) {
      // alert("当前环境uat");
      return 'https://bxuatapi.bisinuolan.cn/api/bihs/auth/taobao/create?code=' + code + '&state=' + platform
    } else if (platform.match('prod')) {
      // alert("当前环境prod");
      return 'https://bxapi.bisinuolan.cn/api/bihs/auth/taobao/create?code=' + code + '&state=' + platform
    } else {
      // alert("未传平台参数");
      $p1.innerHTML = '授权失败'
      $p2.innerHTML = '未传平台参数'
      $error.style.display = 'block'
      $p2.style.display = 'block'
      $maskWrapper.style.display = 'block'
      $authorizationWrapper.style.display = 'none'
      return
    }
  }
  var url = getApiUrl()

  console.log('url', url)
  // alert("当前淘宝api", url);
  $btn.addEventListener('click', function() {
    NextStep()
  })
  if (!url) {
    $p1.innerHTML = '授权失败'
    $p2.innerHTML = '未传平台参数code,state'
    $error.style.display = 'block'
    $p2.style.display = 'block'
    $maskWrapper.style.display = 'block'
    $authorizationWrapper.style.display = 'none'
    return
  }
  var XHR = new XMLHttpRequest()
  XHR.addEventListener('error', function() {
    $error.style.display = 'block'
    $p1.innerHTML = '授权失败'
    $p2.innerHTML = '淘宝授权失败'
    $p2.style.display = 'block'
    $maskWrapper.style.display = 'block'
    $authorizationWrapper.style.display = 'none'
  })
  XHR.open('get', url, true)
  XHR.onreadystatechange = function(e) {
    if (this.readyState == 4 && this.status == 200) {
      var data = JSON.parse(this.response)
      if (data.code == 0 && data.data) {
        $success.style.display = 'block'
        $p1.innerHTML = '授权成功'
      } else {
        $error.style.display = 'block'
        $p1.innerHTML = '授权失败'
        if (data.msg) {
          $p2.innerHTML = data.msg
        } else {
          $p2.innerHTML = '淘宝授权失败'
        }
        $p2.style.display = 'block'
      }
      $maskWrapper.style.display = 'block'
      $authorizationWrapper.style.display = 'none'
    }
  }
  XHR.send()
}
